package leetcode.code1770;

import java.util.HashMap;
import java.util.Map;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution2 implements IDebug {
	int[] nums, multipliers;
	int m, n;

	public int maximumScore(int[] nums, int[] multipliers) {
		this.nums = nums;
		this.multipliers = multipliers;
		m = multipliers.length;
		n = nums.length;
//		Integer[][] dp = new Integer[n][m];
		Map<Integer, Map<Integer, Integer>> dp = new HashMap<>();
		int ans = this.maximumScore(0, 0, n - 1, dp);
		return ans;
	}

	private int maximumScore(int index, int l, int r, Map<Integer, Map<Integer, Integer>> dp) {
		if (index == m) {
			return 0;
		}
		if (dp.containsKey(index) && dp.get(index).containsKey(l)) {
			return dp.get(index).get(l);
		}
//		if (dp[l][index] != null) {
//			return dp[l][index];
//		}
		int cur = this.multipliers[index];
		int ans1 = cur * this.nums[l] + this.maximumScore(index + 1, l + 1, r, dp);
		int ans2 = cur * this.nums[r] + this.maximumScore(index + 1, l, r - 1, dp);
		int ans = Math.max(ans1, ans2);
//		dp[l][index] = ans;
		Map<Integer, Integer> map = dp.getOrDefault(index, new HashMap<>());
		map.put(l, ans);
		dp.put(index, map);
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(14, this.maximumScore(H.str2array3("[1,2,3]"), H.str2array3("[3,2,1]")));
		H.compare(102, this.maximumScore(H.str2array3("[-5,-3,-3,-2,7,1]"), H.str2array3("[-10,-5,3,4,6]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
